package com.yswy.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.dto.req.PackageManageAddUpdateVo;
import com.yswy.backstage.dto.req.PackageManageListVo;
import com.yswy.backstage.entity.user.User;
import com.yswy.backstage.service.PackageManageService;
import com.yswy.common.utils.HuaWeiYunOBSUtil;
import com.yswy.common.utils.StringUtils;
import com.yswy.common.utils.redis.RedisConstants;
import com.yswy.common.utils.redis.RedisUtil;
import com.yswy.domain.entity.PackageManage;
import com.yswy.domain.mapper.PackageManageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Slf4j
@Service
public class PackageManageServiceImpl extends ServiceImpl<PackageManageMapper, PackageManage>
        implements PackageManageService {
    @Autowired
    private HuaWeiYunOBSUtil huaWeiYunOBSUtil;
    @Autowired
    private RedisUtil redisUtil ;
    @Override
    public Page<PackageManage> listPackageManage(PackageManageListVo packageManageListVo ) {
        Page<PackageManage> page = page(new Page<>(packageManageListVo.getPageNum(), packageManageListVo.getPageSize()),
                new LambdaQueryWrapper<PackageManage>()
                        .eq(PackageManage::getIsDel , 0)
                        .like(StringUtils.isNotEmpty(packageManageListVo.getChannle()) , PackageManage::getChannel , packageManageListVo.getChannle())
                        .eq(packageManageListVo.getIsForce() != null , PackageManage::getIsForce , packageManageListVo.getIsForce())
                        .ge(packageManageListVo.getStaTime()!= null ,PackageManage::getCreateTime , packageManageListVo.getStaTime())
                        .le(packageManageListVo.getEndTime()!= null ,PackageManage::getCreateTime , packageManageListVo.getEndTime())
                        .orderByDesc(PackageManage::getId));
        return page;
    }

    @Override
    public void addOrUpdate(PackageManageAddUpdateVo packageManageAddUpdateVo, User user) {
        PackageManage packageManage = new PackageManage();
        if (packageManageAddUpdateVo.getId() != null){
            //更新
            packageManage = getById(packageManageAddUpdateVo.getId());
            packageManage.setCreateTime(new Date());
            packageManage.setCreateBy(user.getId().longValue());
        }else {
            packageManage.setUpdateTime(new Date());
            packageManage.setUpdateBy(user.getId().longValue());
        }
        packageManage.setAppendInformation(packageManageAddUpdateVo.getAppendInformation());
        packageManage.setChannel(packageManageAddUpdateVo.getChannel());
        if (packageManageAddUpdateVo.getStatus() != null){
            packageManage.setStatus(packageManageAddUpdateVo.getStatus());
        }
        if (packageManageAddUpdateVo.getIsForce() != null){
            packageManage.setIsForce(packageManageAddUpdateVo.getIsForce());
        }
        packageManage.setVersion(packageManageAddUpdateVo.getVersion());
        packageManage.setVersionCode(packageManageAddUpdateVo.getVersionCode());
        packageManage.setIsOpenFreeNum(packageManageAddUpdateVo.getIsOpenFreeNum());
        packageManage.setIsOpenFreeNumAd(packageManageAddUpdateVo.getIsOpenFreeNumAd());
        packageManage.setIsCallBackDisposable(packageManageAddUpdateVo.getIsCallBackDisposable());
        packageManage.setShowVipWindow(packageManageAddUpdateVo.getShowVipWindow());
        packageManage.setFileUrl(huaWeiYunOBSUtil.toUrl(packageManageAddUpdateVo.getApkFile()));
        saveOrUpdate(packageManage);
        redisUtil.delete(RedisConstants.packChannle+packageManageAddUpdateVo.getChannel());
    }

    @Override
    public void delPackageManage(Long Id , User user) {
        update(new LambdaUpdateWrapper<PackageManage>().eq(PackageManage::getId , Id)
                .set(PackageManage::getUpdateBy , user.getId())
                .set(PackageManage::getUpdateTime , new Date())
                .set(PackageManage::getIsDel , 1));

    }
}
